package com.citcc.api.controller;

import com.citcc.api.config.Authorization;
import com.citcc.api.utils.BindAlipayServiceUtil;
import com.citcc.business.bean.CdzAppUser;
import com.citcc.business.bean.dto.BindAlipayUserInfo;
import com.citcc.business.bean.vo.PersonalCenterAlipayLoginParamVO;
import com.citcc.business.bean.vo.PersonalCenterInformationVO;
import com.citcc.business.service.PersonalCenterService;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.msg.Response;
import com.citcc.common.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by czh on 2019/5/22 15:57
 */
@Api(value = "个人中心", tags = "个人中心")
@RestController
@RequestMapping("/personalCenter")
public class PersonalCenterController {

    @Autowired
    private PersonalCenterService personalCenterService;
    @Autowired
    private BindAlipayServiceUtil bindAlipayService;

    /**
     * 我的资料
     *
     * @param user
     * @return
     */
    @PostMapping("v1/information")
    @ApiOperation(value = "我的资料", notes = "需要在header中加入Authorization。")
    public Response<PersonalCenterInformationVO> information(@ApiIgnore @Authorization CdzAppUser user) {
        return ResponseUtil.success(personalCenterService.information(user.getId()));
    }

    /**
     * 修改资料
     *
     * @param user
     * @param headUrl
     * @param name
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "headUrl", value = "头像"),
            @ApiImplicitParam(name = "name", value = "昵称")
    })
    @PostMapping("v1/updateInformation")
    @ApiOperation(value = "修改资料", notes = "需要在header中加入Authorization。")
    public Response<Void> updateInformation(@ApiIgnore @Authorization CdzAppUser user,
                                            @RequestParam(value = "headUrl", required = false) String headUrl,
                                            @RequestParam(value = "name", required = false) String name) {
        personalCenterService.updateInformation(user.getId(), headUrl, name);
        return ResponseUtil.success(null);
    }

    /**
     * 微信绑定
     *
     * @param user
     * @param account
     * @param openid
     * @param name
     * @param headUrl
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号"),
            @ApiImplicitParam(name = "openid", value = "OPENID", required = true),
            @ApiImplicitParam(name = "name", value = "昵称", required = true),
            @ApiImplicitParam(name = "headUrl", value = "头像", required = true)
    })
    @PostMapping("v1/bindWeChat")
    @ApiOperation(value = "微信绑定", notes = "需要在header中加入Authorization。请求参数客户端调用微信SDK获取")
    public Response<Void> bindWeChat(@ApiIgnore @Authorization CdzAppUser user,
                                     @RequestParam(value = "account", required = false) String account,
                                     @RequestParam("openid") String openid,
                                     @RequestParam("name") String name,
                                     @RequestParam("headUrl") String headUrl) {

        personalCenterService.bindWeChat(user.getId(), account, openid, name, headUrl);
        return ResponseUtil.success(null);
    }

    /**
     * 支付宝绑定
     *
     * @param user
     * @param oauthCode
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oauthCode", value = "OAUTHCODE", required = true)
    })
    @PostMapping("v1/bindAlipay")
    @ApiOperation(value = "支付宝绑定", notes = "需要在header中加入Authorization。请求参数客户端调用支付宝SDK获取")
    public Response<Void> bindAlipay(@ApiIgnore @Authorization CdzAppUser user,
                                     @RequestParam("oauthCode") String oauthCode) {

        BindAlipayUserInfo alipayUserInfo = bindAlipayService.alipayToken(oauthCode);
        if (alipayUserInfo == null) return ResponseUtil.fail(null);

        personalCenterService.bindAlipay(user.getId(), null, alipayUserInfo.getUserId(), alipayUserInfo.getNickName(), alipayUserInfo.getAvatar());
        return ResponseUtil.success(null);
    }

    /**
     * 解绑微信或支付宝
     *
     * @param user
     * @param type
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "解绑类型 0-支付宝 1-微信", required = true)
    })
    @PostMapping("v1/unBind")
    @ApiOperation(value = "解绑微信或支付宝", notes = "需要在header中加入Authorization。")
    public Response<Void> unBind(@ApiIgnore @Authorization CdzAppUser user, @RequestParam("type") String type) {
        personalCenterService.unBind(user.getId(), type);
        return ResponseUtil.success(null);
    }

    /**
     * 获取绑定支付宝参数
     *
     * @return
     */
    @PostMapping("thirdparty/param")
    @ApiOperation(value = "获取绑定支付宝参数")
    public Response<PersonalCenterAlipayLoginParamVO> alipayLoginParam() {
        return ResponseUtil.success(bindAlipayService.alipayLoginParam());
    }

    /**
     * 内部调用，昵称修改编码昵称
     *
     * @return
     */
    @PostMapping("updateNickNameEncode")
    @ApiOperation(value = "内部调用，昵称修改编码昵称")
    public AjaxResult updateNickNameEncode() {
        personalCenterService.updateNickNameEncode();
        return AjaxResult.success();
    }
}